package net.projects.pro2_atm;

import java.util.ArrayList;
import java.util.List;

public class Bank {
    private List<BankAccount> accounts=new ArrayList<>();

    public Bank(){
        for (int i = 0; i < 10; i++) {
            accounts.add(new BankAccount(i,10));
        }
    }
    public BankAccount serch(int id){
        for (BankAccount account : accounts) {
            if(account.getId()==id){
                return account;
            }
        }
      throw new RuntimeException("查无此帐户");
    }
    public BankAccount deposit(int id,double money){
        BankAccount ba = serch(id);
        synchronized (ba) {
            ba.setBalance(ba.getBalance()+money);
        }
        return ba;
    }
    public BankAccount withdraw(int id,double money){
        BankAccount ba=serch(id);
        synchronized (ba) {
            if(ba.getBalance()<money){
                throw new RuntimeException("余额不足");
            }
            ba.setBalance(ba.getBalance()-money);
        }
        return ba;
    }

}
